package rhinestone.TUI;

import rhinestone.Control.CustomerController;
import rhinestone.Model.Customer;

/**
 * Customer menu UI.
 *
 * @author Group 2
 * @version 2010.12.17
 */
public class CustomerUI extends UI {
	private static final String GENERAL_DISCOUNT_MESSAGE = "General discount: ";
	private static final String CRAFTSMAN_DISCOUNT_MESSAGE = "Craftsman discount: ";
	private static final String DELIVERY_DISCOUNT_MESSAGE = "Delivery discount: ";
	private CustomerController controller;

	public CustomerUI(CustomerController controller) {
		this.controller = controller;
	}

	public void startMenu() {
		boolean finished = false;
		while(!finished) {
			printMenu();
			int choice = promptChoiceInput();
			switch(choice) {
				case 1:
					addCustomerChoice();
					break;
				case 2:
					printCustomerChoice();
					break;
				case 3:
					editCustomerChoice();
					break;
				case 4:
					deleteCustomerChoice();
					break;
				case 5:
					finished = true;
					break;
				default:
					break;
			}
		}
	}

	protected void printMenu() {
		System.out.println(" ----- CRUD CUSTOMER MENU -----");
		System.out.println("\t(-1-) Add customer");
		System.out.println("\t(-2-) Print customer");
		System.out.println("\t(-3-) Edit customer");
		System.out.println("\t(-4-) Delete customer");
		System.out.println("\t(-5-) Back");
	}

	private void addCustomerChoice() {
		String name = promptStringInput(NAME_MESSAGE);
		String address = promptStringInput(ADDRESS_MESSAGE);
		String cpr = promptStringInput(CPR_MESSAGE);
		String phone = promptStringInput(PHONE_MESSAGE);
		double generalDiscount = promptDoubleInput(GENERAL_DISCOUNT_MESSAGE);
		double craftsmanDiscount = promptDoubleInput(CRAFTSMAN_DISCOUNT_MESSAGE);
		double deliveryDiscount = promptDoubleInput(DELIVERY_DISCOUNT_MESSAGE);
		controller.addCustomer(name, address, cpr, phone, generalDiscount, craftsmanDiscount, deliveryDiscount);
	}

	private void printCustomerChoice() {
		String cpr = promptStringInput(CPR_MESSAGE);
		Customer customer = controller.getCustomer(cpr);
		printCustomer(customer);
	}

	private void editCustomerChoice() {
		String cpr = promptStringInput(CPR_MESSAGE);
		Customer customer = controller.deleteCustomer(cpr);
		if(customer != null) {
			System.out.println(" --- EDIT CUSTOMER ---");
			System.out.println("Leave field blank to keep the existing value.");
			System.out.println("Old name: " + customer.getName());
			String name = promptStringInput(NAME_MESSAGE);
			System.out.println("Old address: " + customer.getAddress());
			String address = promptStringInput(ADDRESS_MESSAGE);
			System.out.println("Old CPR: " + customer.getCpr());
			cpr = promptStringInput(CPR_MESSAGE);
			System.out.println("Old phone nr: " + customer.getPhone());
			String phone = promptStringInput(PHONE_MESSAGE);
			System.out.println("Old general discount: " + customer.getGeneralDiscount());
			String generalDiscount = promptStringInput(GENERAL_DISCOUNT_MESSAGE);
			System.out.println("Old crafstman discount: " + customer.getCraftsmanDiscount());
			String craftsmanDiscount = promptStringInput(CRAFTSMAN_DISCOUNT_MESSAGE);
			System.out.println("Old delivery discount: " + customer.getDeliveryDiscount());
			String deliveryDiscount = promptStringInput(DELIVERY_DISCOUNT_MESSAGE);

			controller.updateCustomer(customer, name, address, cpr, phone, generalDiscount, craftsmanDiscount, deliveryDiscount);
		}
		else
			System.err.println("No such customer.");
	}

	private void deleteCustomerChoice() {
		String cpr = promptStringInput(CPR_MESSAGE);
		Customer customer = controller.deleteCustomer(cpr);
		if(customer != null)
			System.out.println("Customer deleted.");
		else
			System.err.println("No such customer.");
	}

	private void printCustomer(Customer customer) {
		if(customer != null)
			System.out.println(customer);
		else
			System.err.println("No such customer.");
	}

	public Customer getCustomer(String cpr) {
		return controller.getCustomer(cpr);
	}
}
